home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Snippets / Toolbox & IAC / AppleEvents / AECDEV⁄AEDAEMON / AEDaemonAEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-01  |  4.4 KB  |  129 lines  |  [TEXT/MPS ]

  1. /* Here are the AppleEvent handlers for this background-only task. */
  2. /* Since this is a real live application, it can accept and send any */
  3. /* Apple Events you want to send or recieve.  */
  4. /* In this case, the only ones that make any sense in this app are */
  5. /* 'oapp' and 'quit' */
  6. #define __BUILDINGDEAMON__
  7. #define __DAE__
  8.  
  9. #include "NonAppAEVT.h"
  10. extern Boolean gQuit;
  11. extern EventRecord ERecord;
  12. extern Boolean gHasAppleEvents;
  13.  
  14. /* a little struct to install handlers from.  Makes it easier to plug in */
  15. /* new handlers */
  16. struct AEinstalls {
  17.     AEEventClass theClass;
  18.     AEEventID theEvent;
  19.     EventHandlerProcPtr theProc;
  20. };
  21. typedef struct AEinstalls AEinstalls;
  22.  
  23. /* InitAEStuff checks for the availability of the AppleEvent Manager and */
  24. /* installs our event handlers. */
  25. /* if the AEM isn't around, we bail. */
  26. void InitAEStuff(void)
  27. {
  28.     static AEinstalls HandlersToInstall[] =  {
  29.          {
  30.             kCoreEventClass, kAEOpenApplication, AEOpenHandler
  31.         },  {
  32.             kCoreEventClass, kAEOpenDocuments, AEOpenDocHandler
  33.         },  {
  34.             kCoreEventClass, kAEQuitApplication, AEQuitHandler
  35.         },  {
  36.             kCoreEventClass, kAEPrintDocuments, AEPrintHandler
  37.         }, 
  38.         /* The above are the four required AppleEvents. */
  39.         
  40.         
  41.     };
  42.     
  43.     OSErr aevtErr = noErr;
  44.     long aLong = 0;
  45.     Boolean gHasAppleEvents = false;
  46.     /* Check this machine for AppleEvents.  If they are not here (ie not 7.0)
  47.     *   then we exit */
  48.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &aLong) == noErr);
  49.     /* The following series of calls installs all our AppleEvent Handlers.
  50.     *   These handlers are added to the application event handler list that 
  51.     *   the AppleEvent manager maintains.  So, whenever an AppleEvent happens
  52.     *   and we call AEProcessEvent, the AppleEvent manager will check our
  53.     *   list of handlers and dispatch to it if there is one.
  54.     */
  55.     if (gHasAppleEvents) {
  56.         register qq;
  57.         for (qq = 0; qq < ((sizeof(HandlersToInstall) / sizeof(AEinstalls))); qq++) {
  58.             aevtErr = AEInstallEventHandler(HandlersToInstall[qq].theClass, HandlersToInstall[qq].theEvent,
  59.                                             HandlersToInstall[qq].theProc, 0, false);
  60.             if (aevtErr) {
  61.                 /* Of course, you can't tell the user why you died directly, since you have no face.  */
  62.                 /* But, you can post a notification from here to let them know. */
  63.                 ExitToShell();                              /* just fail, baby */
  64.             }
  65.         }
  66.     } else {
  67.         ExitToShell();
  68.     }
  69. }
  70.  
  71. /* end InitAEStuff */
  72.  
  73. void DoHighLevel(EventRecord *AERecord)
  74. {
  75.     /* I'm not doing any error handling here because there's not a lot */
  76.     /* I can do, just pass the errors back. */
  77.     AEProcessAppleEvent(AERecord);
  78.     
  79. }
  80.  
  81. /* end DoHighLevel */
  82.  
  83.  
  84. /* This is the standard Open Application event.   */
  85. pascal OSErr AEOpenHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  86. {
  87. #pragma unused (messagein,reply,refIn)
  88.     /* we of course don't do anything here, since we're background only */
  89.     return(noErr);
  90. }
  91.  
  92. /* end AEOpenHandler */
  93.  
  94. /* Open Doc, opens our documents. */
  95. /* In this case, of course, you are in the background so you should return an error */
  96. /* here since you're not opening a document. */
  97. /* Of course, you _might_ want to open a doc, but you will probably */
  98. /* confuse the user if you do, since they will see no action as the */
  99. /*  result of their clicking on a document icon. */
  100. pascal OSErr AEOpenDocHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  101. {
  102. #pragma unused (reply, refIn,messagein)
  103.     /* we of course don't do anything here, so tell the sender that we  */
  104.     /* didn't handle the event */
  105.     return(errAEEventNotHandled);
  106. }
  107.  
  108. pascal OSErr AEPrintHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  109. {
  110. #pragma unused (reply,refIn,messagein)
  111.     /* we of course don't do anything here */
  112.     return(errAEEventNotHandled);
  113. }
  114.  
  115. /* Standard Quit event handler, to handle a Quit event from the Finder, for example.  */
  116. /* ••••• DO NOT CALL EXITTOSHELL HERE ••••• or you will never have a happy life.  */
  117. pascal OSErr AEQuitHandler(AppleEvent *messagein, AppleEvent *reply, long refIn)
  118. {
  119. #pragma unused (messagein,refIn,reply)
  120.     /*  This does _NOT_ quit, you */
  121.     /* should NEVER quit from an AppleEvent handler.  Calling */
  122.     /* ExitToShell here would blow things up */
  123.     gQuit = true;
  124.     return(noErr);
  125. }
  126.  
  127.  
  128. #undef __DAE__
  129. #undef __BUILDINGDEAMON__